package cn.pxs.controller;

import cn.pxs.entity.User;
import cn.pxs.mapper.UserMapper;
import cn.pxs.service.UserService;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/adm/user")
public class UserController {
    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping("/findAll")
    //键值对
    public Map findAll(Integer start,Integer num){
        if(start==null&&num==null){
            return RespJson.pack(500,"别搞啊",null);
        }
        Map map = new HashMap();
        map.put("total",userService.findByLine());
        map.put("users",userService.findAll(start, num));
        return RespJson.pack(200,"查询全用户",map);
    }

    @ResponseBody
    @RequestMapping("/addAdm")
    //键值对 添加管理员
    public Map addAdm(User user){
        if(user.getPassword()==null || user.getAccount()==null){
            return RespJson.pack(400,"账号密码不正确",null);
        }
        userService.addAdm(user);
        return RespJson.pack(200,"添加用户",null);
    }

    @ResponseBody
    @RequestMapping("/resetPwd/{uid}")
    //地址栏绑定uid
    public Map updatePwd(@PathVariable("uid")Integer id){
        int i = userService.updatePwd("111111", id,0);
        if(i==0){
            return RespJson.pack(404,"重置密码失败",null);
        }
        return RespJson.pack(200,"重置密码111111",null);
    }

    @ResponseBody
    @RequestMapping("/close/{uid}")
    //冻结账号
    public Map updateClosed(@PathVariable("uid")Integer id){
        int i = userService.updateClosed(1, id);
        if (i==0){
            return RespJson.pack(404,"冻结账号失败",null);
        }
        return RespJson.pack(200,"冻结账号了..",null);
    }

    @ResponseBody
    @RequestMapping("/findById/{id}")
    //查询该用户信息
    public Map findById(@PathVariable("id")Integer id){
        return RespJson.pack(200,"你查询的用户",userService.findById(id));
    }



}



